package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.CountEnum;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.security.NoSuchAlgorithmException;

/**
 * @author liyannian
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("测试/通过id查用户列表")
    @GetMapping("/test")
    @ResponseBody
    public User personalPage(){
        return userService.getUser();
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse register(@RequestParam("userName") String userName,
                                    @RequestParam("password")String password) throws NoSuchAlgorithmException {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME_AND_PASSWORD);
        }
        if (password.length() < CountEnum.EIGHT.getCount()) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        userService.register(userName,password);
        return ApiRestResponse.success();
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestParam("userName") String userName,
                                 @RequestParam("password")String password, HttpSession session) throws NoSuchAlgorithmException {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME_AND_PASSWORD);
        }
        User user = userService.login(userName,password);
        //保存信息时，不保存密码
        user.setPassword(null);
        session.setAttribute(Constant.IMOOC_MALL_USER,user);
        return ApiRestResponse.success(user);
    }

    @ApiOperation("用户修改个性签名")
    @PostMapping("/user/update")
    @ResponseBody
    public ApiRestResponse updateUserInfo(HttpSession session,@RequestParam("signature") String signature){
       User currentUser = (User)session.getAttribute(Constant.IMOOC_MALL_USER);
       if (currentUser == null) {
           return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
       }
       User user = new User();
       user.setId(currentUser.getId());
       user.setPersonalizedSignature(signature);
       userService.updateInformation(user);
       return ApiRestResponse.success();
    }

    @ApiOperation("用户登出")
    @PostMapping("/user/logout")
    @ResponseBody
    public ApiRestResponse logout(HttpSession session) {
        session.removeAttribute(Constant.IMOOC_MALL_USER);
        return ApiRestResponse.success();
    }

    @ApiOperation("管理员登录")
    @PostMapping("/adminLogin")
    @ResponseBody
    public ApiRestResponse adminLogin(@RequestParam("userName") String userName,
                                 @RequestParam("password")String password, HttpSession session) throws NoSuchAlgorithmException {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME_AND_PASSWORD);
        }
        User user = userService.login(userName,password);
        if (userService.checkAdminRole(user)) {
            //保存信息时，不保存密码
            user.setPassword(null);
            session.setAttribute(Constant.IMOOC_MALL_USER,user);
            return ApiRestResponse.success(user);
        }else {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
    }
}
